import Vue from 'vue'
import Vuex from 'vuex'
import {levels} from "../util/skinTestQuestions";
import {baseUrl,imgPrefixUrl} from "../myConfig";
import {simpleCopyBySrc} from "../util/util";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    username: '',
    nickname: '',
    gender: '',
    birthday:'',
    rank: '',
    status: '',
    uid: '',
    remember: '',
    avatar: '',
    email: '',
    phone:'',
    description: '',
    skinType:'',
    privilege:'',
  },
  mutations: {
    setToken(state,token) {
      state.token = token
      window.localStorage.setItem('token',token)
    },
    loadToken(state){
      state.token = window.localStorage.getItem("token") ? window.localStorage.getItem("token") : ''
    },
    init(state, data) {
      simpleCopyBySrc(data,state)
      state.birthday = state.birthday ? state.birthday.split(' ')[0] : ''
      state.gender = state.gender+''
      state.privilege = parseInt(state.privilege)
    },
    quit(state) {
      state.nickname = ''
      state.username = ''
      state.gender = ''
      state.rank = ''
      state.status = ''
      state.uid = ''
      state.createtime = ''
      state.token = ''
      state.email = ''
      state.phone = ''
      state.gender = '0'
      state.description = ''
      state.skinType = ''
      state.remember = ''
      state.privilege = ''
      window.localStorage.removeItem("token")
      window.localStorage.removeItem("remember")
    },
    setSkinType(state,skinType){
      state.skinType = skinType
    }
  },
  getters: {
    hasLogin: (state) => {
      return state.token !== ''
    },
    hasProfile:(state)=>{
      return state.email !== '' && state.gender !== 0
    },
    isAdmin:(state)=>{
      return state.privilege && state.privilege > 1
    },
    getScores:(state)=>{
      if (state.skinType){
        return state.skinType.split("|")
      }else{
        return undefined
      }
    },
    getAvatar:(state)=>{
      if (state.avatar){
        return baseUrl+imgPrefixUrl+state.avatar
      }else{
        return ''
      }
    }
  },


  actions: {},
  modules: {}
})
